var Qixi = function() {
     // 开始播放音乐
    // if (config.audio.enable) { 
    //     var audio1 = Hmlt5Audio(config.audio.playURl);
    //     audio1.end(function() {
    //         Hmlt5Audio(config.audio.cycleURL, true);
    //     });
    // }

 ///////////
 //灯动画 //
 ///////////
 var lamp = {
     elem: $('.b_background'),
     bright: function() {
         this.elem.addClass('lamp-bright');
     },
     dark: function() {
         this.elem.removeClass('lamp-bright');
     }
 };

 //var container = $("#content");
// var swipe = Swipe(container);
  // 用来临时调整页面
 //swipe.scrollTo(visualWidth * 2, 0);
 //visualWidth = container.width();
 //visualHeight = container.height();

 // 页面滚动到指定的位置
 function scrollTo(time, proportionX) {
     var distX = visualWidth * proportionX;
     swipe.scrollTo(distX, time);
 }

 // 获取数据
 var getValue = function(className) {
     var $elem = $('' + className + '');
         //走路的路线坐标
     return {
         height: $elem.height(),
         top: $elem.position().top
     };
 };

 // 桥的Y轴
 var bridgeY = function() {
     var data = getValue('.c_background_middle');
     return data.top;
 }();

 ////////
 //小女孩 //
 ////////
 var girl = {
     elem: $('.girl'),
     getHeight: function() {
         return this.elem.height();
     },
      rotate: function() {
          this.elem.addClass("girl-rotate")
      },     
     setOffset: function() {
         this.elem.css({
             left: visualWidth / 2,
             top: bridgeY - this.getHeight()
         });
     },
      getOffset: function() { return this.elem.offset() },
      getWidth: function() { return this.elem.width() }     
 };

 // 修正小女孩位置
 girl.setOffset();

  var bird = { elem: $(".bird"), fly: function() { this.elem.addClass("birdFly");
            this.elem.transition({ right: visualWidth }, 15000, "linear") } };
  var logo = { elem: $(".logo"), run: function() { this.elem.addClass("logolightSpeedIn").on(animationEnd, function() { $(this).addClass("logoshake").off() }) } };
  var boy = WalkBoy();
    boy.walkTo(config.setTime.walkToThird, 0.6)
      .then(function() { scrollTo(config.setTime.walkToMiddle, 1); return boy.walkTo(config.setTime.walkToMiddle, 0.5) })
      .then(function() { bird.fly() })
      .then(function() { boy.stopWalk(); return BoyToShop(boy) })
      .then(function() { girl.setOffset();
        scrollTo(config.setTime.walkToEnd, 2); return boy.walkTo(config.setTime.walkToEnd, 0.15) })
      .then(function() { return boy.walkTo(config.setTime.walkTobridge, 0.25, (bridgeY - girl.getHeight()) / visualHeight) })
      .then(function() { var proportionX = (girl.getOffset().left - boy.getWidth() - instanceX + girl.getWidth() / 5) / visualWidth; return boy.walkTo(config.setTime.bridgeWalk, proportionX) })
      .then(function() { boy.resetOriginal();
        setTimeout(function() { girl.rotate();
            boy.rotate(function() { logo.run();
                snowflake() }) }, config.setTime.waitRotate) });


 function doorAction(left, right, time) {
     var $door = $('.door');
     var doorLeft = $('.door-left');
     var doorRight = $('.door-right');
     var defer = $.Deferred();
     var count = 2;
     // 等待开门完成
     var complete = function() {
         if (count == 1) {
             defer.resolve();
             return;
         }
         count--;
     }
     doorLeft.transition({
         'left': left
     }, time, complete);
     doorRight.transition({
         'left': right
     }, time, complete);
     return defer;
 }

 // 开门
 function openDoor() {
     return doorAction('-50%', '100%', 2000);
 }

 // 关门
 function shutDoor() {
     return doorAction('0%', '50%', 2000);
 }
    var BoyToShop = function(boyObj) {
        var defer = $.Deferred();
        var $door = $(".door");
        var doorLeft = $(".door-left");
        var doorRight = $(".door-right");

        function doorAction(left, right, time) { var defer = $.Deferred(); var count = 2; var complete = function() { if (count == 1) { defer.resolve(); return } count-- };
            doorLeft.transition({ "left": left }, time, complete);
            doorRight.transition({ "left": right }, time, complete); return defer }

        function openDoor(time) { return doorAction("-50%", "100%", time) }

        function shutDoor(time) { return doorAction("0%", "50%", time) }

        function talkFlower() { var defer = $.Deferred();
            boyObj.talkFlower();
            setTimeout(function() { defer.resolve() }, config.setTime.waitFlower); return defer }
        var lamp = {
            elem: $(".b_background"),
            bright: function() {
                this.elem.addClass("lamp-bright")
            },
            dark: function() { this.elem.removeClass("lamp-bright") }
        };
        var waitOpen = openDoor(config.setTime.openDoorTime);
        waitOpen.then(function() { lamp.bright(); return boyObj.toShop($door, config.setTime.walkToShop) }).then(function() { return talkFlower() }).then(function() { return boyObj.outShop(config.setTime.walkOutShop) }).then(function() { shutDoor(config.setTime.shutDoorTime);
            lamp.dark();
            defer.resolve() });
        return defer
    };

    function snowflake() { var $flakeContainer = $("#snowflake");

        function getImagesName() { return config.snowflakeURl[[Math.floor(Math.random() * 6)]] }

        function createSnowBox() { var url = getImagesName(); return $('<div class="snowbox" />').css({ "width": 41, "height": 41, "position": "absolute", "backgroundSize": "cover", "zIndex": 100000, "top": "-41px", "backgroundImage": "url(" + url + ")" }).addClass("snowRoll") } setInterval(function() { var startPositionLeft = Math.random() * visualWidth - 100,
                startOpacity = 1;
            endPositionTop = visualHeight - 40, endPositionLeft = startPositionLeft - 100 + Math.random() * 500, duration = visualHeight * 10 + Math.random() * 5000; var randomStart = Math.random();
            randomStart = randomStart < 0.5 ? startOpacity : randomStart; var $flake = createSnowBox();
            $flake.css({ left: startPositionLeft, opacity: randomStart });
            $flakeContainer.append($flake);
            $flake.transition({ top: endPositionTop, left: endPositionLeft, opacity: 0.7 }, duration, "ease-out", function() { $(this).remove() }) }, 200) }


    /////////
    //背景音乐 //
    /////////
    // function Hmlt5Audio(url, isloop) {
    //     var audio = new Audio(url);
    //     audio.autoPlay = true;
    //     audio.loop = isloop || false;
    //     audio.play();
    //     return {
    //         end: function(callback) {
    //             audio.addEventListener('ended', function() {
    //                 callback();
    //             }, false);
    //         }
    //     };
    // }
}